/*
 * Copyright 2013 Glassy Cinema.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package hu.glassy.core.model;

import hu.glassy.model.common.SortableAbstractEntity;
import java.util.HashSet;
import java.util.Set;
import javax.persistence.CascadeType;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.FetchType;
import javax.persistence.JoinColumn;
import javax.persistence.ManyToOne;
import javax.persistence.OneToMany;
import javax.persistence.OrderColumn;
import javax.persistence.Table;
import javax.persistence.UniqueConstraint;

/**
 * Represents a Code which can be used generally in the application
 *
 * @author Alexander Molnár <molnar.alexander@sonrisa.hu>
 */
@Entity
@Table(name = "code", uniqueConstraints =
{
  @UniqueConstraint(columnNames =
  {
    "name", "parent_id"
  })
})
public class Code extends SortableAbstractEntity
{

  public static final int NAME_LENGTH = 64;

  @Column(name = "name", length = NAME_LENGTH, nullable = false, unique = false)
  private String name;

  @Column(name = "nameresource", length = 1024, nullable = false, unique = false)
  private String nameResource;

  @ManyToOne()
  @JoinColumn(name = "parent_id", referencedColumnName = "id", nullable = true, unique = false)
  private Code parent;

  @OneToMany(mappedBy = "parent", fetch = FetchType.LAZY, cascade = CascadeType.ALL)
  @OrderColumn(name = "priority")
  private Set<Code> cilds = new HashSet<Code>();

  /**
   * @return the name
   */
  public String getName()
  {
    return name;
  }

  /**
   * @param name the name to set
   */
  public void setName(String name)
  {
    this.name = name;
  }

  /**
   * @return the nameResource
   */
  public String getNameResource()
  {
    return nameResource;
  }

  /**
   * @param nameResource the nameResource to set
   */
  public void setNameResource(String nameResource)
  {
    this.nameResource = nameResource;
  }

  /**
   * @return the parent
   */
  public Code getParent()
  {
    return parent;
  }

  /**
   * @param parent the parent to set
   */
  public void setParent(Code parent)
  {
    this.parent = parent;
  }

  /**
   * @return the cilds
   */
  public Set<Code> getCilds()
  {
    return cilds;
  }

  /**
   * @param cilds the cilds to set
   */
  public void setCilds(Set<Code> cilds)
  {
    this.cilds = cilds;
  }

}
